package com.xoa.service.performance;

import com.xoa.dao.performance.ScoreFlowMapper;
import com.xoa.dao.performance.ScoreGroupMapper;
import com.xoa.dao.performance.ScoreItemMapper;
import com.xoa.dao.performance.ScoreSelfDataMapper;
import com.xoa.dao.scoreflowitem.ScoreFlowItemMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.common.SysPara;
import com.xoa.model.performance.*;
import com.xoa.model.scoreflowitem.ScoreFlowItem;
import com.xoa.model.users.Users;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class ScoreSelfDataService {
    @Autowired
    private ScoreSelfDataMapper scoreSelfDataMapper;
    @Autowired
    private ScoreFlowMapper scoreFlowMapper;
    @Autowired
    private ScoreFlowItemMapper scoreFlowItemMapper;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private ScoreItemMapper scoreItemMapper;

    @Autowired
    private ScoreGroupMapper scoreGroupMapper;


    /**
     * 郭心雨
     * 2018.06.25
     * 添加个人评价
     * @param scoreSelfData
     * @return
     */
    public ToJson addScoreSelfData(ScoreSelfDataWithBLOBs scoreSelfData) {
        ToJson toJson=new ToJson();
        int count=scoreSelfDataMapper.insertSelective(scoreSelfData);
        if (count>0){
            toJson.setMsg("ok");
            toJson.setFlag(0);
        }else{
            toJson.setMsg("err");
            toJson.setFlag(1);
        }
        return toJson;
    }

    /**
     * 郭心雨
     * 2018.06.25
     * 删除个人评价
     * @param sid
     * @return
     */
    public ToJson delScoreSelfData(Integer sid) {
        ToJson toJson=new ToJson();
        int count=scoreSelfDataMapper.deleteByPrimaryKey(sid);
        if (count>0){
            toJson.setMsg("ok");
            toJson.setFlag(0);
        }else{
            toJson.setMsg("err");
            toJson.setFlag(1);
        }
        return toJson;
    }

    /**
     * 郭心雨
     * 2018.06.25
     * 修改个人评价
     * @param scoreSelfData
     * @return
     */
    public ToJson modifyScoreSelfData(ScoreSelfDataWithBLOBs scoreSelfData) {
        ToJson toJson=new ToJson();
        int count=scoreSelfDataMapper.updateByPrimaryKeySelective(scoreSelfData);
        if (count>0){
            toJson.setMsg("ok");
            toJson.setFlag(0);
        }else{
            toJson.setMsg("err");
            toJson.setFlag(1);
        }
        return toJson;
    }

    /**
     * 郭心雨
     * 2018.06.25
     * 查看个人评价
     * @param scoreSelfData
     * @param page
     * @param pageSize
     * @param useFlag
     * @return
     */
    public ToJson selScoreSelfData(ScoreSelfDataWithBLOBs scoreSelfData, Integer page, Integer pageSize, Boolean useFlag) {
        ToJson<ScoreSelfDataWithBLOBs> toJson=new ToJson<>();
        PageParams pageParams=new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        Map<String,Object> map =new HashMap<>();
        map.put("page",pageParams);
        map.put("scoreSelfData",scoreSelfData);
        List<ScoreSelfDataWithBLOBs> scoreSelfDataList=scoreSelfDataMapper.selScoreSelfData(map);

        if (scoreSelfDataList.size()>0){
            toJson.setObj(scoreSelfDataList);
            toJson.setTotleNum(pageParams.getTotal());
            toJson.setMsg("ok");
            toJson.setFlag(0);
        }else{
            toJson.setMsg("err");
            toJson.setFlag(1);
        }
        return toJson;
    }

    public ToJson saveSelScore(HttpServletRequest request, String jsonStr){
        ToJson toJson=new ToJson();
        try{
            List<ScoreFlowItem> list=(List<ScoreFlowItem>) JSONArray.toList(JSONArray.fromObject(jsonStr), ScoreFlowItem.class);
            if(list.size()>=0 && list!=null){
                for(ScoreFlowItem scoreFlowItem:list){
                    Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
                    scoreFlowItem.setExaminer(user.getUserId());
                    scoreFlowItem.setExaminerStr(user.getUserName());
                    //把信息存放到数据库
                    scoreFlowItemMapper.insertSelective(scoreFlowItem);
                }
                ScoreFlowItem sco=new ScoreFlowItem();
                sco.setFlowId(list.get(0).getFlowId());
                sco.setGroupId(list.get(0).getGroupId());
                //当前scoreFlowItem表中存放  考核信息  的个数
                sco.setSelfFlag(1);
                List<ScoreFlowItem> listKaoHe=scoreFlowItemMapper.getScoreFlowItemByFlowID(sco);
                //当前scoreFlowItem表中存放  自评信息  的个数
                sco.setSelfFlag(0);
                List<ScoreFlowItem> listZiPing=scoreFlowItemMapper.getScoreFlowItemByFlowID(sco);


                //被考核人列表
                ScoreFlowWithBLOBs scoreFlowWithBLOBs=scoreFlowMapper.getKaoheRenList(list.get(0).getFlowId());
                String[] parts=scoreFlowWithBLOBs.getParticipant().split(",");
                //指标集明细个数
                int count =scoreItemMapper.selCountByGroupId(list.get(0).getGroupId());
                //修改自评状态
                if(list.get(0).getSelfFlag() == 0){
                     //需要自评的条数=明细个数
                    if(count==listZiPing.size()) {
                        //修改自评状态
                        scoreFlowMapper.updateZipingStatusByFlowId(list.get(0).getFlowId());
                    }
                //修改考核状态
                }else if(list.get(0).getSelfFlag() == 1){
                    //需要考核的条数=被考核人个数*指标集明细个数     *考核人个数
                    if((parts.length*count)==listKaoHe.size()) {
                        //修改考核状态
                        scoreFlowMapper.updeteKaoheStatusByFlowId(list.get(0).getFlowId());
                    }
                }
            }
            toJson.setMsg("true");
            toJson.setFlag(0);
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
            toJson.setFlag(1);
        }
        return toJson;
    }

    public ToJson<ScoreFlowItem> getScoreFlowItemByFlowId(ScoreFlowItem scoreFlowItem){
        ToJson<ScoreFlowItem>  toJson=new ToJson<>();
        try{
            List<ScoreFlowItem> list=scoreFlowItemMapper.getScoreFlowItemByFlowID(scoreFlowItem);
            for(ScoreFlowItem s:list){
                String name=scoreItemMapper.getItemNameByItemId(s.getItemId());
                s.setItemName(name);
            }

            if(list.size()>0){
                toJson.setObj(list);
                toJson.setObject("1");
                toJson.setMsg("true");
                toJson.setFlag(0);
            }else{
                toJson.setObj(list);
                toJson.setObject("0");
                toJson.setMsg("true");
                toJson.setFlag(0);
            }

        }catch (Exception e){
            toJson.setMsg(e.getMessage());
            toJson.setFlag(1);
        }
        return toJson;
    }

    public ToJson getKaoheRenList(int flowId){
        ToJson toJson=new ToJson();
        try{
            List<ScoreFlow> bkhrList=new ArrayList<>();
            ScoreFlowWithBLOBs scoreFlowWithBLOBs=new ScoreFlowWithBLOBs();
            if(flowId!=0){
                //获取考核人List
                scoreFlowWithBLOBs=scoreFlowMapper.getKaoheRenList(flowId);
                String participant=scoreFlowWithBLOBs.getParticipant();
                String bkh[]=participant.split(",");
                for(String s:bkh){
                    //得到考核人name
                    ScoreFlowWithBLOBs scoreFlowWithBLOBsl=new ScoreFlowWithBLOBs();
                    scoreFlowWithBLOBsl.setGroupId(scoreFlowWithBLOBs.getGroupId());
                    scoreFlowWithBLOBsl.setFlowId(scoreFlowWithBLOBs.getFlowId());
                    scoreFlowWithBLOBsl.setParticipant(s);
                    scoreFlowWithBLOBsl.setParticipantName(usersMapper.getUsernameByUserId(s));
                    bkhrList.add(scoreFlowWithBLOBsl);
                }
            }
            toJson.setObject(scoreFlowWithBLOBs.getFlowTitle());
            toJson.setObj(bkhrList);
            toJson.setMsg("true");
            toJson.setFlag(0);
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
            toJson.setFlag(1);
        }
        return toJson;
    }

    public ToJson getKaohe(ScoreFlowItem scoreFlow){
        ToJson toJson=new ToJson();
        try{
            List list=new LinkedList();
            //全部被考核人
            StringBuffer  allList=new StringBuffer();
            //已考核
            StringBuffer yList=new StringBuffer();
            //未考核
            StringBuffer  wList=new StringBuffer();
            //获取考核人List
            ScoreFlowWithBLOBs scoreFlowWithBLOBs=scoreFlowMapper.getKaoheRenList(scoreFlow.getFlowId());

            String b[]=scoreFlowWithBLOBs.getParticipant().split(",");
            for(String s:b){
                String ss=usersMapper.getUsernameByUserId(s);
                if(!StringUtils.checkNull(ss)){
                    allList.append(ss+",");
                }else{
                    allList.append("用户不存在,");
                }
            }
            //判断用户是否考核
            for(String s:b){
               int count= scoreFlowItemMapper.getKaoheRen(scoreFlow);
               if(count ==0 ){
                  String name=usersMapper.getUsernameByUserId(s);
                  if(!StringUtils.checkNull(name)){
                      wList.append(name+",");
                  }else{
                      wList.append("用户不存在,");
                  }
               }else{
                  String name=usersMapper.getUsernameByUserId(s);
                  if(!StringUtils.checkNull(name)){
                      yList.append(name+",");
                  }else{
                      yList.append("用户不存在,");
                  }
               }
            }
            list.add(allList.toString());
            list.add(wList.toString());
            list.add(yList.toString());
            toJson.setObj(list);
            toJson.setFlag(0);
            toJson.setMsg("true");
        }catch (Exception e){
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    public ToJson getIsKaohe(ScoreFlowItem scoreFlowItem){
        ToJson toJson=new ToJson();
        try {
            int count= scoreFlowItemMapper.getKaoheRen(scoreFlowItem);
            if(count>0){
                //说明考核完了展示详情
                toJson.setObject("true");

            }else{
                //可以考核
                toJson.setObject("false");
            }
            toJson.setMsg("true");
            toJson.setFlag(0);
        }catch (Exception e){
           toJson.setMsg(e.getMessage());
           toJson.setFlag(1);
        }
        return toJson;
    }

    public ToJson<ScoreFlowItem> getScoreFlowItemByZiPing(ScoreFlowItem scoreFlowItem,String type) {
        ToJson<ScoreFlowItem>  toJson=new ToJson<>();
        try{
            List<ScoreFlowItem> list=scoreFlowItemMapper.getScoreFlowItemByFlowID(scoreFlowItem);

        }catch (Exception e){
            toJson.setMsg(e.getMessage());
            toJson.setFlag(1);
        }
        return toJson;
    }
}
